package cn.ahaoweb.drule.core.listener;

import cn.ahaoweb.drule.core.FactInfo;
import org.kie.api.event.rule.*;

/**
 * @Name: RuleExecuteListener
 * @Description:
 * @Author: wangzhonghao
 * @Date: 2025/2/13 15:31
 */
public class DefaultRuleExecuteListener extends DefaultAgendaEventListener implements RuleExecuteListener {

    private FactInfo factInfo;

    private RuleExecuteListener ruleExecuteListener;

    public DefaultRuleExecuteListener() {
    }

    public DefaultRuleExecuteListener(FactInfo factInfo) {
        this.factInfo = factInfo;
    }

    public DefaultRuleExecuteListener(FactInfo factInfo, RuleExecuteListener ruleExecuteListener) {
        this.factInfo = factInfo;
        this.ruleExecuteListener = ruleExecuteListener;
    }

    /**
     * 取消条件判断
     * <pre>
     *     规则条件判断结束，如果不满足则触发此事件
     * </pre>
     *
     * @param event
     */
    public void matchConditionCancelled(MatchCancelledEvent event, FactInfo factInfo) {
        ruleExecuteListener.matchConditionCancelled(event, factInfo);
    }

    /**
     * 开始条件判断
     * <pre>
     *    开始判断规则的条件是否满足时触发
     * </pre>
     *
     * @param event
     */
    public void matchConditionStart(MatchCreatedEvent event, FactInfo factInfo) {
        ruleExecuteListener.matchConditionStart(event, factInfo);
    }

    /**
     * 触发规则结果前
     * <pre>
     *    当该规则条件满足，准备执行此规则结果前触发
     * </pre>
     *
     * @param event
     */
    public void beforeFireRule(BeforeMatchFiredEvent event, FactInfo factInfo) {
        ruleExecuteListener.beforeFireRule(event, factInfo);
    }

    /**
     * 触发规则结果后
     * <pre>
     *    当该规则条件满足，执行此规则结果后触发
     * </pre>
     *
     * @param event
     */
    public void afterFireRule(AfterMatchFiredEvent event, FactInfo factInfo) {
        ruleExecuteListener.afterFireRule(event, factInfo);
    }

    @Override
    public boolean singleton() {
        return ruleExecuteListener.singleton();
    }

    /**
     * 获取事实信息
     *
     * @return
     */
    public FactInfo getFactInfo() {
        return factInfo;
    }

    public void setFactInfo(FactInfo factInfo) {
        this.factInfo = factInfo;
    }

    public RuleExecuteListener getRuleExecuteListener() {
        return ruleExecuteListener;
    }

    public void setRuleExecuteListener(RuleExecuteListener ruleExecuteListener) {
        this.ruleExecuteListener = ruleExecuteListener;
    }

    @Override
    public void matchCancelled(MatchCancelledEvent event) {
        matchConditionCancelled(event, factInfo);
    }

    @Override
    public void matchCreated(MatchCreatedEvent event) {
        matchConditionStart(event, factInfo);
    }

    @Override
    public void afterMatchFired(AfterMatchFiredEvent event) {
        afterFireRule(event, factInfo);
    }

    @Override
    public void beforeMatchFired(BeforeMatchFiredEvent event) {
        beforeFireRule(event, factInfo);
    }

}
